package com.virtualperson.util;

import com.virtualperson.dto.StyleGenerationRequest;
import com.virtualperson.exception.ImageValidationException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class StyleValidationUtil {
    // 支持的风格索引值
    private static final Set<Integer> VALID_STYLE_INDICES = new HashSet<>(Arrays.asList(
        -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    ));

    // 默认模型
    private static final String DEFAULT_MODEL = "wanx-style-repaint-v1";

    /**
     * 验证风格索引值
     * @param styleIndex 风格索引值
     * @throws ImageValidationException 如果值无效
     */
    public static void validateStyleIndex(Integer styleIndex) {
        if (styleIndex == null) {
            throw new ImageValidationException("style_index参数不能为空");
        }
        
        if (!VALID_STYLE_INDICES.contains(styleIndex)) {
            throw new ImageValidationException(
                String.format("style_index参数值必须为：%s", String.join("、", 
                    VALID_STYLE_INDICES.stream().map(String::valueOf).toArray(String[]::new)))
            );
        }
    }

    /**
     * 验证风格参考图片URL
     * @param styleIndex 风格索引值
     * @param styleRefUrl 风格参考图片URL
     * @throws ImageValidationException 如果验证失败
     */
    public static void validateStyleRefUrl(Integer styleIndex, String styleRefUrl) {
        if (styleIndex == -1 && (styleRefUrl == null || styleRefUrl.trim().isEmpty())) {
            throw new ImageValidationException("当style_index为-1时，style_ref_url参数不能为空");
        }
        
        if (styleRefUrl != null && !styleRefUrl.trim().isEmpty()) {
            ImageValidationUtil.validateImage(styleRefUrl, "style_ref_url", false, false, false);
        }
    }

    /**
     * 验证并设置默认参数
     * @param request 风格化生成请求
     */
    public static void validateAndSetDefaults(StyleGenerationRequest request) {
        if (request == null) {
            throw new ImageValidationException("请求参数不能为空");
        }

        // 设置默认模型
        if (request.getModel() == null || request.getModel().trim().isEmpty()) {
            request.setModel(DEFAULT_MODEL);
        }

        // 验证输入参数
        if (request.getInput() == null) {
            throw new ImageValidationException("input参数不能为空");
        }

        // 验证图片URL
        if (request.getInput().getImageUrl() == null || request.getInput().getImageUrl().trim().isEmpty()) {
            throw new ImageValidationException("image_url参数不能为空");
        }
        ImageValidationUtil.validateImage(request.getInput().getImageUrl(), "image_url", false, false, false);

        // 验证风格索引
        validateStyleIndex(request.getInput().getStyleIndex());

        // 验证风格参考图片URL
        validateStyleRefUrl(request.getInput().getStyleIndex(), request.getInput().getStyleRefUrl());
    }
} 